﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;


namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        //界面刷新
        public static int bs = 0;

        //学生界面刷新
        private void sqlconn()
        {
            SqlConnection con = new SqlConnection("server=.;database=期末实训;pwd=123456;uid=sa");
            con.Open();
            SqlCommand com = new SqlCommand("select a.*,b.className from Student as a join Class as b on a.StuClassID=b.classID", con);
            SqlDataAdapter sda = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            sda.Fill(ds, "ds");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "ds";
            con.Close();
            groupBox1.Text = "学生信息";
        }


        //班级界面刷新
        private void sqljm()
        {
            SqlConnection con = new SqlConnection("server=.;database=期末实训;pwd=123456;uid=sa;");
            con.Open();
            SqlCommand com = new SqlCommand("select * from Class", con);
            SqlDataAdapter sda = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            sda.Fill(ds, "ds");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "ds";
            con.Close();
            con.Dispose();
            groupBox1.Text = "班级信息";
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            sqljm();
        }


        public Form1()
        {
            InitializeComponent();
            timer1.Start();
            sqlconn();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }


        private void DataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (groupBox1.Text == "班级信息")
            {
                addclass a = new addclass();
                a.Show();
            }
            else
            {
                addStu a = new addStu();
                a.Show();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            //根据查询条件显示
            SQLmanager m = new SQLmanager();
            dataGridView1.DataSource = m.Select(name);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

       


        private void button6_Click(object sender, EventArgs e)
        {
            sqlconn();

        }

        private void button5_Click(object sender, EventArgs e)
        {
            登入 d = new 登入();
            d.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (bs == 1)
            {
                if (groupBox1.Text == "学生信息")
                {
                    sqlconn();
                }
                else
                {
                    sqljm();
                }
                bs = 0;
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (groupBox1.Text == "班级信息")
            {
                try
                {
                    int index = dataGridView1.CurrentRow.Index;    //取得选中行的索引  
                    string s = dataGridView1.Rows[index].Cells[1].Value.ToString();
                    SQLmanager d = new SQLmanager();
                    d.DeleteC(s);
                    MessageBox.Show("删除成功！");
                    bs = 1;
                }
                catch(Exception x)
                {
                    MessageBox.Show(x.Message+" 请重新选择！");
                }

            }
            else
            {
                int index = dataGridView1.CurrentRow.Index;    //取得选中行的索引  
                string s = dataGridView1.Rows[index].Cells[1].Value.ToString();
                int b= (int)dataGridView1.Rows[index].Cells[4].Value;
                SQLmanager d = new SQLmanager();
                d.DeleteS(s,b);
                MessageBox.Show("删除成功！");
                bs = 1;
            }
            
        }

        private void 修改_Click(object sender, EventArgs e)
        {
            if (groupBox1.Text == "学生信息")
            {
                int index = dataGridView1.CurrentRow.Index;    //取得选中行的索引  
                List<object> list = new List<object> {
                dataGridView1.Rows[index].Cells[0].Value.ToString(),
                dataGridView1.Rows[index].Cells[1].Value.ToString(),
                dataGridView1.Rows[index].Cells[2].Value.ToString(),
                dataGridView1.Rows[index].Cells[3].Value.ToString(),
                dataGridView1.Rows[index].Cells[4].Value.ToString()
                };
                SQLmanager s = new SQLmanager();
                s.Updata(list);
                MessageBox.Show("修改成功！");
                bs = 1;
            }
            else
            {
                int index = dataGridView1.CurrentRow.Index;    //取得选中行的索引  
                List<object> list = new List<object> {
                dataGridView1.Rows[index].Cells[0].Value.ToString(),
                dataGridView1.Rows[index].Cells[1].Value.ToString(),
                };
                SQLmanager s = new SQLmanager();
                s.UpdataC(list);
                MessageBox.Show("修改成功！");
                bs = 1;
            }
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
